package com.onccc.social.step

import android.os.Build
import android.view.accessibility.AccessibilityNodeInfo
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ScreenUtils
import com.onccc.social.Assists
import com.onccc.social.Assists.click
import com.onccc.social.Assists.getBoundsInScreen
import com.onccc.social.Assists.getChildren
import com.onccc.social.Assists.log
import com.onccc.social.Assists.paste
import com.onccc.social.AssistsWindowManager
import com.onccc.social.MainActivity
import com.onccc.social.MainActivity.Companion.getComment
import com.onccc.social.MainActivity.Companion.getFiveTime
import com.onccc.social.MainActivity.Companion.getInputTime
import com.onccc.social.MainActivity.Companion.getInterest
import com.onccc.social.MainActivity.Companion.getOneTime
import com.onccc.social.MainActivity.Companion.getTwoTime
import com.onccc.social.OverManager
import com.onccc.social.model.CommentModel
import com.onccc.social.model.ImageInterestModel
import com.onccc.social.stepper.Step
import com.onccc.social.stepper.StepCollector
import com.onccc.social.stepper.StepImpl
import com.onccc.social.stepper.StepManager
import kotlinx.coroutines.delay
import kotlin.random.Random


/**
 * @author GongChengHao
 * @date 2024/12/4
 * Description :
 */
class YouTubeSocial : StepImpl() {

    var mPackageName = "com.google.android.youtube"

    private var maxSelect = 3
    private var selectIndex = 0
    private var mCommentModel = CommentModel()
    private var mImageInterestModel = ImageInterestModel()
    private var videoIndex = 0

    private var getShort = 0

    override fun onImpl(collector: StepCollector) {
        collector.next(StepTag.STEP_1) {
            OverManager.log("启动youtube")

            AppUtils.launchApp(mPackageName)
            runIO { delay(getTwoTime()) }

            Assists.findByText("Accept").forEach() {
                runIO { delay(1000) }
                it.click()
            }
            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_2) {
            runIO { delay(getTwoTime()) }
            Assists.findByText("Search").forEach() {
                OverManager.log("点击搜索")
                it.click()
                runIO { delay(getTwoTime()) }
            }
            return@next Step.get(StepTag.STEP_3)
        }.next(StepTag.STEP_3) {
            OverManager.log("输入搜索内容")
            runIO { delay(getTwoTime()) }
            Assists.findByTags("android.widget.EditText").forEach {
                it.click()
                it.performAction(AccessibilityNodeInfo.ACTION_FOCUS)
                runIO { delay(getOneTime()) }
                for (s in MainActivity.getKeyword()) {
                    it.paste(s.toString())
                    runIO { delay(MainActivity.getInputTime()) }
                }
                runIO { delay(getOneTime()) }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                    it.performAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_IME_ENTER.id)
                }
                runIO { delay(getTwoTime()) }

            }

            return@next Step.get(StepTag.STEP_4)
        }.next(StepTag.STEP_4) {
            runIO { delay(getTwoTime()) }
            AssistsWindowManager.switchNotTouchableAll()
            val xL = java.util.Random().nextInt(ScreenUtils.getAppScreenWidth()).toFloat()
            val xR = java.util.Random().nextInt(ScreenUtils.getAppScreenWidth()).toFloat()
            val distance = ScreenUtils.getAppScreenHeight() / 5F
            val startY = distance + java.util.Random().nextInt(200).toFloat() + 300
            val endY = distance - java.util.Random().nextInt(100).toFloat()
            OverManager.log("滑动：$xL$startY,$xR/$endY")
            Assists.gesture(
                    floatArrayOf(xL, endY), floatArrayOf(xR, startY), 0, 800L + java.util.Random().nextInt(800)
            )
            AssistsWindowManager.switchTouchableAll()

            return@next Step.get(StepTag.STEP_21)
        }.next(StepTag.STEP_21) {
            runIO { delay(getTwoTime()) }
            getShort = 0
            Assists.findByText("Shorts").forEach() {
                val screen = it.getBoundsInScreen()
                if (screen.bottom < 500 && screen.top < 500) {
                    OverManager.log("查看短视频列表")
                    it.parent.click()
                }
                runIO { delay(getTwoTime()) }
                return@next Step.get(StepTag.STEP_22)
            }

            Assists.findById("com.google.android.youtube:id/search_clear").forEach() {
                runIO { delay(getOneTime()) }
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_3)
            }

            runIO { delay(getTwoTime()) }
            return@next Step.get(StepTag.STEP_4)
        }.next(StepTag.STEP_22) {
            runIO { delay(getTwoTime()) }
            AssistsWindowManager.switchNotTouchableAll()
            val xL = java.util.Random().nextInt(ScreenUtils.getAppScreenWidth()).toFloat()
            val xR = java.util.Random().nextInt(ScreenUtils.getAppScreenWidth()).toFloat()
            val startY = ScreenUtils.getAppScreenHeight() - java.util.Random().nextInt(300).toFloat() - 200
            val endY = java.util.Random().nextInt(500).toFloat() + 200
            OverManager.log("滑动：$xL$startY,$xR/$endY")
            Assists.gesture(
                    floatArrayOf(xL, startY), floatArrayOf(xR, endY), 0, 300L + java.util.Random().nextInt(500)
            )
            AssistsWindowManager.switchTouchableAll()
            if (getShort < 1) {
                getShort++
                return@next Step.get(StepTag.STEP_22)
            } else {
                return@next Step.get(StepTag.STEP_6)
            }
        }.next(StepTag.STEP_5) {

            selectIndex++
            if (selectIndex > maxSelect) {
                runIO { delay(getTwoTime()) }
                Assists.back()
                return@next Step.get(StepTag.STEP_100)
            }

            runIO { delay(getFiveTime()) }
            runIO { delay(getTwoTime()) }
            OverManager.log("向下滑动")
            AssistsWindowManager.switchNotTouchableAll()
            runIO { delay(856) }
            val x = ScreenUtils.getAppScreenWidth() / 2F
            val distance = ScreenUtils.getAppScreenHeight() / 2F
            val startY = distance + distance / 2F
            val endY = distance - distance / 2F
            OverManager.log("滑动：$x/$startY,$x/$endY")
            Assists.gesture(
                    floatArrayOf(x, startY), floatArrayOf(x, endY), 0, 1500L
            )
            AssistsWindowManager.switchTouchableAll()

            return@next Step.get(StepTag.STEP_7)

        }.next(StepTag.STEP_6) {
            runIO { delay(getOneTime()) }

            val x = 100 + java.util.Random().nextInt(ScreenUtils.getAppScreenWidth() - 200).toFloat()
            val y = 400 + java.util.Random().nextInt(ScreenUtils.getAppScreenHeight() - 800).toFloat()
            val clickTime = 34L + java.util.Random().nextInt(40)
            OverManager.log("点击：$x,$y")
            Assists.gestureClick(x, y, clickTime)
            runIO { delay(getTwoTime()) }

            Assists.findById("com.google.android.youtube:id/elements_button_bar_container").firstOrNull() {
                return@next Step.get(StepTag.STEP_7)
            }

            return@next Step.get(StepTag.STEP_6)
        }.next(StepTag.STEP_7) {
            getInterest({ interestModel ->
                mImageInterestModel = interestModel
                StepManager.execute(YouTubeSocial::class.java, StepTag.STEP_8, begin = true)
            })
            {
                StepManager.execute(YouTubeSocial::class.java, StepTag.STEP_5, begin = true)

            }
            return@next Step.none
        }.next(StepTag.STEP_8) {
            var time = 0
            if (mImageInterestModel.isToLike) {
                time = 3000 + Random.nextInt(5000)
            }
            if (mImageInterestModel.isToFollow) {
                time += Random.nextInt(5000)
            }
            if (mImageInterestModel.isToComment) {
                time += Random.nextInt(5000)
            }
            runIO { delay(time.toLong()) }

            if (mImageInterestModel.isToLike) {
                OverManager.log("点赞")
                runIO { delay(getOneTime()) }
                Assists.findById("com.google.android.youtube:id/elements_button_bar_container").forEach {
                    it.log()
                    val first = it.getChildren()[0]
                    val second = first.getChildren()[0]
                    val third = second.getChildren()[0]
                    val btnLike = third.getChildren()[0]
                    btnLike.click()
                    runIO { delay(getTwoTime()) }
                    return@next Step.get(StepTag.STEP_9)
                }
            }
            return@next Step.get(StepTag.STEP_9)
        }.next(StepTag.STEP_9) {
            if (mImageInterestModel.isToFollow) {
                runIO { delay(getOneTime()) }
                Assists.findById("com.google.android.youtube:id/metapanel").forEach {
                    val first = it.getChildren()[0]
                    val second = first.getChildren()[1]
                    val third = second.getChildren()[1]
                    third.click()
                    runIO { delay(getTwoTime()) }
                    return@next Step.get(StepTag.STEP_10)
                }
            }
            return@next Step.get(StepTag.STEP_10)
        }.next(StepTag.STEP_10) {
            if (mImageInterestModel.isToComment) {
                return@next Step.get(StepTag.STEP_11)
            }
            return@next Step.get(StepTag.STEP_5)
        }.next(StepTag.STEP_11) {
            runIO { delay(getOneTime()) }
            OverManager.log("查找评论按钮")
            Assists.findById("com.google.android.youtube:id/elements_button_bar_container").forEach {
                it.log()
                val first = it.getChildren()[0]
                val second = first.getChildren()[2]
                val third = second.getChildren()[0]
                val btnLike = third.getChildren()[0]
                btnLike.click()
                runIO { delay(getTwoTime()) }
                return@next Step.get(StepTag.STEP_12)
            }
            runIO { delay(getTwoTime()) }

            return@next Step.get(StepTag.STEP_5)
        }.next(StepTag.STEP_12) {
            runIO { delay(getOneTime()) }

            getComment({ commentModel ->
                mCommentModel = commentModel
                StepManager.execute(YouTubeSocial::class.java, StepTag.STEP_13, begin = true)
            })
            {
                StepManager.execute(YouTubeSocial::class.java, StepTag.STEP_15, begin = true)
            }
            return@next Step.none
        }.next(StepTag.STEP_13) {
            runIO { delay(getOneTime()) }
            OverManager.log("发送评论")
            if (mCommentModel.comment.isNullOrEmpty()) {
                return@next Step.get(StepTag.STEP_14)
            }
            Assists.findByTags("android.widget.EditText").forEach {
                it.log()
                Assists.gestureViewClick(it)

                runIO { delay(getTwoTime()) }
                Assists.findByTags("android.widget.EditText").forEach { it1 ->
                    it1.performAction(AccessibilityNodeInfo.ACTION_FOCUS)
                    for (s in mCommentModel.comment) {
                        it1.paste(s.toString())
                        runIO { delay(getInputTime()) }
                    }
                }
                return@next Step.get(StepTag.STEP_14)
            }
            return@next Step.get(StepTag.STEP_14)
        }.next(StepTag.STEP_14) {
            OverManager.log("点击发送")
            runIO { delay(getOneTime()) }
            Assists.findById("com.google.android.youtube:id/interstitials_container").forEach {
                it.log()
                val first = it.getChildren()[0]
                val second = first.getChildren()[0]
                val third = second.getChildren()[2]
                third.click()
                runIO { delay(getTwoTime()) }
                return@next Step.get(StepTag.STEP_15)
            }
            return@next Step.get(StepTag.STEP_15)
        }.next(StepTag.STEP_15) {
            runIO { delay(getTwoTime()) }
            OverManager.log("确认是否发送成功")
            var isSend = false
            Assists.findById("com.google.android.youtube:id/close_button").firstOrNull {
                isSend = true
                it.click()
            }
            runIO { delay(getTwoTime()) }
            if (isSend) {
                return@next Step.get(StepTag.STEP_5)
            } else {
                return@next Step.get(StepTag.STEP_15)
            }
        }.next(StepTag.STEP_100) {
            OverManager.log("结束")
            return@next Step.none
        }
    }
}